/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package venndiagram;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

/**
 * Canvas to draw the VennDiagram with data specified in GraphPanel
 * 
 * @author Adam
 */
public class VennCanvas extends Canvas {

    //ArrayLists to hold all of the shape and color objects to be drawn.
    //The shape any any given index of shapes corresponds with the color
    //at the same index. So, shapes.get(i) should be drawn with the color
    //at colors.get(i)
    private ArrayList<Shape> shapes;
    private ArrayList<Color> colors;
    private ArrayList<String> setNames;
    private ArrayList<Integer> counts;
    private int additionalHeight = 15;
    private boolean textOn = true;
    private boolean textInCenter = true;

    public VennCanvas(ArrayList<Shape> shapes, ArrayList<Color> colors) {
        //instantiate lists
        this.shapes = shapes;
        this.colors = colors;
        //set a static size for the canvas
        this.setSize(476, 357);
        //set the background color to white
        this.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        //Typecast g to a Graphics2D object to draw shapes
        Graphics2D g2d = (Graphics2D) g;

        //If there are shapes and colors to draw
        if (shapes != null && colors != null) {
            //Walk through both lists (which should be of equal size)
            //changing the color to and drawing the shape at i of both lists
            for (int i = 0; i < shapes.size(); i++) {
                //Check if a shape is null because data sets of two and three
                //use the same size list and some items are null
                if (shapes.get(i) != null) {
                    g2d.setColor(colors.get(i));
                    g2d.fill(shapes.get(i));
                }
            }


            if (textOn) {
                //Draw the names of the two/three main sets and the count of
                //ever subset
                if (setNames != null && counts != null) {

                    int countsX = 10;
                    int countsY = 245;

                    for (int i = 0; i < shapes.size(); i++) {

                        //draw the current setName
                        if (setNames.get(i) != null) {
                            g2d.setColor(Color.black);
                            Rectangle2D box = shapes.get(i).getBounds2D();
                            g2d.setFont(new Font("SansSerif", Font.PLAIN, 16));
                            if (i < 2) {
                                g2d.drawString(setNames.get(i),
                                        (float) (box.getCenterX() - setNames.get(i).length() * 3),
                                        (float) (box.getMaxY() + 15));
                            } else {
                                g2d.drawString(setNames.get(i),
                                        (float) (box.getCenterX() - setNames.get(i).length() * 3),
                                        //(float)(box.getCenterY() - box.getHeight()/2 - additionalHeight));
                                        (float) box.getMinY() - 5);
                            }
                        }

                        //draw the current setCount, this version draws at the set locations on the canvas
                        if(textInCenter){
                            if(counts.get(i) != null){
                                g2d.setColor(Color.black);
                                Rectangle2D box = shapes.get(i).getBounds2D();
                                g2d.setFont(new Font("SansSerif", Font.PLAIN, 16));
                                g2d.drawString(counts.get(i)+"", 
                                       (float)(box.getCenterX()), 
                                       (float)(box.getCenterY()));
                            }
                        }
                        else{
                            if (counts.get(i) != null) {
                                g2d.setColor(Color.black);
                                g2d.setFont(new Font("SansSerif", Font.PLAIN, 16));
                                if (i < 3) {
                                    g2d.drawString(setNames.get(i) + ": " + counts.get(i), countsX, countsY);
                                } else if (i == 3) {
                                    g2d.drawString(setNames.get(0) + " ∩ " + setNames.get(1) + ": " + counts.get(i), countsX, countsY);
                                } else if (i == 4) {
                                    g2d.drawString(setNames.get(1) + " ∩ " + setNames.get(2) + ": " + counts.get(i), countsX, countsY);
                                } else if (i == 5) {
                                    g2d.drawString(setNames.get(0) + " ∩ " + setNames.get(2) + ": " + counts.get(i), countsX, countsY);
                                } else {
                                    g2d.drawString(setNames.get(0) + " ∩ " + setNames.get(1) + " ∩ " + setNames.get(2) + ": " + counts.get(i), countsX, countsY);
                                }
                                countsY += 17;

                            }
                        }
                    }
                }
            }
        }


    }

    //set the shape arrays
    public void setShapes(ArrayList<Shape> shapeList) {
        shapes = shapeList;
    }

    //set the color arrays
    public void setColors(ArrayList<Color> colorList) {
        colors = colorList;
    }
    
    //set the arraylist which holds the set names
    public void setNames(ArrayList<String> setNameList) {
        setNames = setNameList;
    }

    //set the arraylist which holds the count of all the subsets
    public void setCounts(ArrayList<Integer> setCountList) {
        counts = setCountList;
    }

    //toggles whether the text should be drawn or not
    public void toggleText() {
        textOn = !textOn;
        //repaint to display/remove text from screen
        repaint();
    }
    
    //holds whether text is on or off
    public boolean textIsOn(){
        return textOn;
    }
    
    public void toggleTextInCenter(){
        textInCenter = !textInCenter;
        repaint();
    }

    public boolean textIsInCenter(){
        return textInCenter;
    }
    
    public static void main(String[] args) {
        Frame f = new Frame();
        f.setBounds(20, 20, 100, 100);
        f.setMinimumSize(new Dimension(476, 357));
        GraphDataTwoSets data = new GraphDataTwoSets("a", "b", 30000, 15000, 0);

        Shape a = new Ellipse2D.Double(data.maxCircle.getCenter().x, data.maxCircle.getCenter().y, data.maxCircle.getRadius(), data.maxCircle.getRadius());
        Shape b = new Ellipse2D.Double(data.minCircle.getCenter().x, data.minCircle.getCenter().y, data.minCircle.getRadius(), data.minCircle.getRadius());
        // convert to areas for intersection
        Area aArea = new Area(a);
        Area bArea = new Area(b);
        aArea.intersect(bArea);
        Shape ab = aArea;

        ArrayList<Shape> sarr = new ArrayList<Shape>();
        sarr.add(a);
        sarr.add(b);
        sarr.add(ab);

        ArrayList<Color> carr = new ArrayList<Color>();
        carr.add(Color.red);
        carr.add(Color.green);
        carr.add(Color.blue);

        f.add(new VennCanvas(sarr, carr));
        f.setVisible(true);
    }
}
